<template>
  <div class="container">
    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="search.supName" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="负责人">
        <el-input v-model="search.supPersonInCharge" placeholder="请输入负责人"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="search.supTel" placeholder="请输入电话"></el-input>
      </el-form-item>
      <!--      <el-form-item label="活动区域">-->
      <!--        <el-select v-model="search.region" placeholder="活动区域">-->
      <!--          <el-option label="区域一" value="shanghai"></el-option>-->
      <!--          <el-option label="区域二" value="beijing"></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="button">
      <el-button @click="onAdd" type="primary">新增供应商</el-button>
    </div>
    <el-table
      border
      :data="list"
      style="width: 100%"
      class="table"
    >
      <el-table-column
        prop="supName"
        label="名称"
        width="200">
      </el-table-column>
      <el-table-column
        prop="supPersonInCharge"
        label="负责人"
        width="200">
      </el-table-column>
      <el-table-column
        prop="supTel"
        label="电话"
        width="200">
      </el-table-column>
      <el-table-column
        prop="supAddress"
        label="地址">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.supId)"
          >
            修改
          </el-button>
          <el-popconfirm
            title="确认删除？"
            @confirm="handleDelete(scope.row.supId)"
          >
            <el-button
            slot="reference"
            size="mini"
            type="text"
            icon="el-icon-delete"
          >删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end; margin-top: 20px">
      <el-pagination
        v-show="total>0"
        :total="total"
        background
        @current-change="getList"
        :current-page.sync="search.currentPage"
        :page-size.sync="search.size"
        layout="total, prev, pager, next"
      />
    </div>
  </div>
</template>

<script>
import {deleteSupplier, supplierList} from '@/api/user'
export default {
  name: 'SupplierInfo',
  data() {
    return {
      search: {
        supName: '',
        supPersonInCharge: '',
        supTel: '',
        currentPage: 1,
        size: 10
      },
      total: 0,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      supplierList(this.search).then(res => {
        console.log(res)
        this.list = res.data.records
        this.total = res.data.total
      })
    },
    onAdd() {
      this.$router.push({ name: 'AddSupplier' })
    },
    handleUpdate(id) {
      this.$router.push({ name: 'AddSupplier', query: { id } })
    },
    handleDelete(id) {
      deleteSupplier({ supId: id }).then(res => {
        this.$message({
          message: '刪除成功',
          type: 'success'
        })
        this.getList();
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 50px;
  display: flex;
  flex-direction: column;
  .button {
    display: flex;
    justify-content: flex-end;
  }
  .table {
    margin-top: 20px;
  }
}

</style>
